<!DOCTYPE html>
<html lang="en">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <link rel="stylesheet" type="text/css" href="./css/login2.css">
    <link rel="stylesheet" href="./css/reg.css">
    <script src="../js/jquery.js"></script>
    <script src="../js/appNet.js"></script>
    <script src="../js/constant.js"></script>
    <script src="../js/cookie-2.2.1.min.js"></script>
    <script src="../js/md5.min.js"></script>
    <title>登录</title>
</head>
<body>
<div class="container">
    <div class="login-form">
        <div class="text">登录</div>
        <div class="form-item">
            <input type="text" id="username" placeholder="用户名">
            <div id="usernameHint" class="tel-warn show"
                 style="font-size: 15px;"></div>
            <input type="password" id="password" placeholder="密码">
            <span id="passwordHint" class="tel-warn show"
                  style="font-size: 15px;"></span>
        </div>
        <div id="hint"
             style="color: #da2a2e;font-size: 10px;margin: 0 10%; text-align: center">
        </div>
        <div class="btn" id="login">登录</div>
        <div class="btn-back"></div>
        <div class="other">
            没有账号？<a href="#">请联系管理员</a>
        </div>
    </div>
</div>
</body>
<style>
    .tel-warn {
        color: #ea5d5f;
        font-size: 12px;
        right: 0;
        top: 22px;
    }

    .show {
        display: block !important;
    }
</style>
<script>
    $(function () {
        var username = $("#username");
        var password = $("#password");
        var usernameHint = $("#usernameHint");
        var passwordHint = $("#passwordHint");
        var hint = $("#hint");
        var login = $("#login");

        username.focus(function () {
            usernameHint.text("");
            passwordHint.text("");
        });
        password.focus(function () {
            usernameHint.text("");
            passwordHint.text("");
        });

        login.click(function () {
            toLogin();
        });

        $(document).keyup(function (event) {
            if (event.keyCode == 13) {//回车键
                toLogin();
            }
        });

        function toLogin() {
            if (username.val() == null || username.val() == "") {
                usernameHint.text("请输入账号");
                return;
            }
            if (password.val() == null || password.val() == "") {
                passwordHint.text("请输入密码");
                return;
            }

            console.log("hoset    " + window.location.host);

            hint.text("");
            login.text("登录中");
            login.addClass("off").removeClass("on");

            var pass = md5(password.val()); //56b21847ed32d2d96cf74077b22342eb
            post("/api/auth/user/login",
                {
                    "username": username.val(),
                    "password": pass
                },
                false,
                function onResponseSuccess(res) {
                    login.text("登录成功");
                    login.addClass("on").removeClass("off");
                    var data = res.data;
                    var token = data.token;
                    // Cookies.set('token', token, {expires: 3});
                    //跳转到最后一次访问的地址
                    post("/mgt/session/getLastUrl",
                        {},
                        false,
                        function onResponseSuccess(res) {
                            var url = res.data;
                            if (url.indexOf("swagger-ui.html") > 0) {
                                var flag = false;
                                for (var i = 0; i < data.accessList.length; i++) {
                                    if (data.accessList[i] == 'swaggerReadAccess') {
                                        flag = true;
                                        break;
                                    }
                                }
                                if (flag) {
                                    window.location.href = url;
                                } else {
                                    login.text("登录");
                                    login.addClass("on").removeClass("off");

                                    hint.text("您无权限访问接口文档");
                                }
                            } else {
                                window.location.href = url;
                            }

                        }, function onResponseCodeError(data) {
                            window.location.href = "../index.html";
                        }, function onError(data) {
                            window.location.href = "../index.html";
                        }
                    );
                    // setTimeout(function () {
                    //     // 跳转到逻辑处理页面
                    //     window.location.href = "translate.html";
                    // }, 1000);
                }, function onResponseCodeError(data) {
                    login.text("登录");
                    login.addClass("on").removeClass("off");

                    hint.text(data);
                }, function onError(res) {
                    login.text("登录");
                    login.addClass("on").removeClass("off");

                    hint.text("网络异常-" + res);
                }
            );

            // $.ajax({
            //     url: 'http://localhost:12000/measure/api/auth/user/login',//地址
            //     type: 'post',//类型
            //     timeout: 20000,//超时
            //     data: {
            //         "username": username.val(),
            //         "password": password.val()
            //     },
            //     async: true,
            //     //  默认情况下，标准的跨域请求是不会发送cookie的
            //     dataType: "json",
            //     beforeSend: function (xhr) {
            //         xhr.withCredentials = true;
            //     },
            //     crossDomain: true,
            //     //请求成功
            //     success: function (data, status) {
            //         $.ajax({
            //             url: 'http://localhost:12000/measure/mgt/session/getLastUrl',//地址
            //             type: 'post',//类型
            //             timeout: 20000,//超时
            //             async: true,
            //             //  默认情况下，标准的跨域请求是不会发送cookie的
            //             dataType: "json",
            //             beforeSend: function (xhr) {
            //                 xhr.withCredentials = true;
            //             },
            //             crossDomain: true,
            //             //请求成功
            //             success: function (data, status) {
            //                 window.location.href = "../index.html";
            //             },
            //             //失败/超时
            //             error: function (XMLHttpRequest, textStatus, errorThrown) {
            //                 if (textStatus === 'timeout') {
            //                     alert('請求超時');
            //                     setTimeout(function () {
            //                         alert('重新请求');
            //                     }, 2000);
            //                 }
            //                 //alert(errorThrown);
            //             }
            //         });
            //     },
            //     //失败/超时
            //     error: function (XMLHttpRequest, textStatus, errorThrown) {
            //         if (textStatus === 'timeout') {
            //             alert('請求超時');
            //             setTimeout(function () {
            //                 alert('重新请求');
            //             }, 2000);
            //         }
            //         //alert(errorThrown);
            //     }
            // });
        }

    });
</script>
</html>